package docmanager.operation.novedades;

import java.sql.Connection;
import java.sql.Date;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import docmanager.db.ConnectionPool;

import bf.operation.Operation;
import bf.operation.OperationException;

public class OpAgregarNovedad2 extends Operation {

	@Override
	public Map execute(HttpServletRequest request) throws OperationException {
		
		String texto = request.getParameter("texto");
		java.util.Date fechaHoy = new java.util.Date();
		java.sql.Timestamp momentoTimestamp = new java.sql.Timestamp(fechaHoy.getTime());

		try {
			Connection conn = ConnectionPool.getConnection();
			java.sql.PreparedStatement st = conn.prepareStatement("INSERT INTO novedades (fecha,texto) VALUES (?,?)");
			st.setTimestamp(1, momentoTimestamp);
			st.setString(2, texto);  
			st.executeUpdate();
			ConnectionPool.closeConn(conn);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new OperationException();
		}

		
		Map m = new HashMap();
		m.put("mensaje", "Novedad publicada con exito");
		// TODO Auto-generated method stub
		return m;
	}

	@Override
	public String getReturnURL() {
		// TODO Auto-generated method stub
		return "Main.jsp";
	}

}
